package com.nba.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.service.AdminService;
import com.nba.service.impl.AdminServiceImpl;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("adminName");
		String pwd = req.getParameter("pwd");
		
		if(name==null) {
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
		
		if(pwd != null && pwd!=null &&name!=null) {
			req.getSession().setAttribute("pwd", pwd);
			req.getSession().setAttribute("name", name);
			AdminService adminservice = new AdminServiceImpl();
			int num = adminservice.loginAdmin(name, pwd);
				if(num==1) {
					resp.sendRedirect("SearchServlet.do");
				}
				if(num==0) {
					req.setAttribute("msg1","*用户名不存在");
					req.getRequestDispatcher("login.jsp").forward(req, resp);
				}
				if(num==-1) {
					req.setAttribute("msg2","*密码错误");
					req.getRequestDispatcher("login.jsp").forward(req, resp);
				}

		}
		
	}
	
	
}
